package com.drama.api.sys.organization.controller;

import com.drama.api.base.ApiIndex;
import com.drama.api.base.IDEntity;
import com.drama.api.base.R;
import com.drama.api.base.RestPage;
import com.drama.api.sys.organization.domain.vo.*;
import com.drama.api.sys.organization.service.ISysOrganizationService;
import com.drama.util.BindingResultUtil;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


@Api(description = "系统机构相关", tags = ApiIndex.SysOrgController)
@RequestMapping(value = "/api/org")
@RestController
public class SysOrganizationController {

    @Autowired
    private ISysOrganizationService service;

    @ApiOperation(value = "查询集合【列表】")
    @GetMapping(value = "/get_list")
    public R<RestPage<List<SysOrganizationVO>>> get_list(
            @ApiParam(value = "查询参数") @ModelAttribute SysOrganizationSearchVO searchVO) {
        List<SysOrganizationVO> list = service.getList(searchVO);
        RestPage restPage = new RestPage( new PageInfo(list),list);
        return new R(restPage);
    }

    @ApiOperation(value = "查询集合【树形】")
    @GetMapping(value = "/get_tree_list")
    public R<List<SysOrganizationVO>> get_tree_list(
            @ApiParam(value = "查询参数") @ModelAttribute SysOrganizationSearchTreeVO searchVO) {
        List<SysOrganizationVO> list = service.getTreeList(searchVO);
        return new R(list);
    }

    @ApiOperation(value = "添加数据")
    @PostMapping(value = "/add")
    public R add(
            @ApiParam(value = "添加参数") @Valid @RequestBody SysOrganizationAddVO addVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.BindingResult(bindingResult);
        }
        return new R().setData(service.add(addVO));
    }

    @ApiOperation(value = "修改数据")
    @PostMapping(value = "/update")
    public R update(
            @ApiParam(value = "修改参数") @Valid @RequestBody SysOrganizationUpdateVO updateVO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return BindingResultUtil.BindingResult(bindingResult);
        }
        service.update(updateVO);
        return new R();
    }

    @ApiOperation(value = "删除数据")
    @PostMapping(value = "delete")
    public R delete( @ApiParam(value = "修改参数") @Valid @RequestBody IDEntity delete) {
        service.delete(delete.getId());
        return new R();
    }

}
